package com.origin.aidllibrary.client;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.origin.aidllibrary.IMyAidlInterface;
import com.origin.aidllibrary.IServiceMsgCallBack;
import com.origin.aidllibrary.R;
import com.origin.aidllibrary.service.MyService;
import com.origin.utils.FileUtils;
import com.origin.utils.PathUtils;
import com.origin.utils.ToastUtils;

import java.io.File;
import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ClientActivity extends AppCompatActivity {

    private IMyAidlInterface iMyAidlInterface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_client);

        Intent intent = new Intent();
        intent.setPackage("com.origin.app");
        intent.setAction("com.origin.aidllibrary.action");
        bindService(intent, new ServiceConnection() {

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {

                iMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        }, Context.BIND_AUTO_CREATE);

    }

    public void sendFile(View view) {

        try {
            if (iMyAidlInterface == null) {
                Log.e("sendFile", "失败");
                return;
            }
            iMyAidlInterface.setServiceMsgCallBack(new IServiceMsgCallBack.Stub() {
                @Override
                public void receiveMessage(String str) throws RemoteException {
                    if (str.equals(MyService.canSendFileTag)) {
                        sendFile();
                    }
                }
            });
            iMyAidlInterface.sendMessage(MyService.startSendFileTag);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    ExecutorService executorService = Executors.newCachedThreadPool();

    static int interval = 1020;


    String filePath = PathUtils.filesPath + "/wallhaven-57o9j5.png";
//    String filePath = PathUtils.filesPath + "/1.txt";

    private void sendFile() throws RemoteException {
        File file = new File(filePath);

        if (iMyAidlInterface == null) {
            return;
        }


        executorService.execute(new Runnable() {
            @Override
            public void run() {
                byte[] fileBytes = FileUtils.fileConvertByteArray(file);

                int start = 0;
                int end = 0;

                while (fileBytes.length > end) {
                    end = start + interval;
                    if (end > fileBytes.length) {
                        end = fileBytes.length;
                    }
                    byte[] newFileBytes = Arrays.copyOfRange(fileBytes, start, end);
                    start += interval;
                    try {
                        iMyAidlInterface.sendMessage(MyService.fileHeadTag + Arrays.toString(newFileBytes) + MyService.fileTailTag);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }


                try {
                    iMyAidlInterface.sendMessage(MyService.sendFileFinishTag);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(ClientActivity.this, "发送成功", Toast.LENGTH_SHORT).show();
                        }
                    });
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        });
//
    }
}